require(['./config'], () => {
    require(['template','url','header','pager'], (template,url,header,pager) => {
        class Cakes {
            constructor () {
                this.addfooter()
                this.addGood()
            }
            
            addfooter(){
                $('footer').load('../html/footer.html')
            }
            addGood(){
                $.get(url.baseUrl + '/cake',resp =>{
                    if(resp.code == 200){
                        let {list} = resp.body
                            this.list = list
                            var _this = this

                        this.totalPage = list.length / 8
                        $("#pagination1").pagination({
                            currentPage: 1,// 当前页数
                            totalPage: this.totalPage,// 总页数
                            isShow: true,// 是否显示首尾页
                            count: 5,// 显示个数
                            homePageText: "首页",// 首页文本
                            endPageText: "尾页",// 尾页文本
                            prevPageText: "上一页",// 上一页文本
                            nextPageText: "下一页",// 下一页文本
                            callback: function(current) {
                                $("#current1").text(current)
                                let curnum = current
                                let arr = []
                                $.each(_this.list,function(i,item){
                                   if( (curnum-1)*8< item.id && item.id<= curnum*8){
                                       arr.push(item)
                                   }
                                })
                                // let res = _this.list.filter(x =>(curnum+1)*8<= x.id <= curnum*8)
                                //给当前点击元素对象添加class .paixu
                                $('.taste,.specs').on('click','li',function(){
                                    $(this).addClass('paixu').siblings().removeClass('paixu')

                                    //价格从低到高排序
                                    if($(this).html() === "价格从低到高"){
                                        arr = arr.sort(function(a,b){
                                            return a.money - b.money 
                                        })
                                    }
                                    //价格从高到低排序
                                    if($(this).html() === "价格从高到低"){
                                        arr = arr.sort(function(a,b){
                                            return b.money - a.money 
                                        })
                                    }
                                })
                                _this.renderGood(arr)
                                header.cardCount()
                            }
                        });
                       
                        let res = this.list.filter(x => x.id <= 8)
                        $('.taste,.specs').on('click','li',function paixu(){
                            $(this).addClass('paixu').siblings().removeClass('paixu')

                            //价格从低到高排序
                            if($(this).html() === "价格从低到高"){
                                res = res.sort(function(a,b){
                                    return a.money - b.money 
                                })
                            }
                            //价格从高到低排序
                            if($(this).html() === "价格从高到低"){
                                res = res.sort(function(a,b){
                                    return b.money - a.money 
                                })
                            }
                        //重新渲染
                        _this.renderGood(res)
                       
                        })
                        this.renderGood(res)
                        header.cardCount()
                    }
                })
            }
            renderGood(list){
                $('.goodlist').html(template('cakegood',{list}))
            }
        }
        new Cakes()
    })
})